﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DynamicType
{
  class Program
  {
    static void Main(string[] args)
    {
      dynamic obj;
      Console.WriteLine("GetObject 1st time");
      obj = GetObject();
      Console.WriteLine("Type ob obj is {0}", obj.GetType());
      obj.Add(5, 4);

      Console.WriteLine("GetObject 2nd time");
      obj = GetObject();
      Console.WriteLine("Type ob obj is {0}", obj.GetType());
      obj.Add(5, 4); // error because obj is type of MyClass2. It don't has Add method 
      Console.ReadKey();
    }

    class MyClass1
    {
      public void Add(int a, int b)
      {
        Console.WriteLine("{0} Add {1} = {2}", a, b, a + b);
      }
    }
    class MyClass2
    { 
    }
    
    static int count = 0;
    static dynamic GetObject()
    {
      count++;
      if (count == 1)
        return new MyClass1();
      else
        return new MyClass2();
    }
  }
}
